#include "app_mqtt.h"

/* 一些要用的宏定义*/

#define ADDRESS "tcp://192.168.0.104:1883"
#define CLIENTID "b253ba38-daf6-4b37-984f-5d8fdc6a1cfb"
#define SUBSCRIBE_TOPIC "R2G" // 订阅的主题
#define PUBLISH_TOPIC "G2R"   // 发送消息的主题
#define QOS 1                               // 至少一次
#define TIMEOUT 10000L

/* MQTT全局变量*/
static MQTTClient mqttClient;
static MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
static MQTTClient_message pubmsg = MQTTClient_message_initializer;


/* 其他模块的接受回调函数 */
mqtt_callback mqtt_recvcallback_func = NULL;

/* 三个回调函数 */
void delivered(void *context, MQTTClient_deliveryToken dt)
{
    log_debug("Message send ok");
}

int msg_arrive(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
    // printf("Message arrived\n");
    // printf("   topic: %s\n", topicName);
    // printf("  message: %.*s\n", message->payloadlen, (char *)message->payload);

    /* 这里用自己的回调函数处理信息 */
    if (mqtt_recvcallback_func != NULL)
    {
        mqtt_recvcallback_func(message);
    }
    


    /* 处理完消息后要释放内存 */
    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);

    return 1; /* 返回值内部要处理 */
}

void connect_lost(void *context, char *cause)
{
    // printf("\nConnection lost\n");
    // printf("   cause: %s\n", cause);
    log_error("Connection lost,cause:%s", cause);
}

returnType_t app_mqtt_init(void)
{
    /* 1.创建客户端 */
    if (MQTTClient_create(&mqttClient, ADDRESS, CLIENTID,
                          MQTTCLIENT_PERSISTENCE_NONE, NULL) != MQTTCLIENT_SUCCESS)
    {
        log_error("MQTTClient_create failed");
        return ERROR;
    }

    /* 2.设置回调函数 */
    if (MQTTClient_setCallbacks(mqttClient, NULL,
                                connect_lost, msg_arrive, delivered) != MQTTCLIENT_SUCCESS)
    {
        log_error("MQTTClient_setCallbacks failed");

        /* 设置失败要destroy mqttclient*/
        MQTTClient_destroy(&mqttClient);
        return ERROR;
    }

    int result = -1;
    /* 3.连接MQTT—Broker服务器*/
    conn_opts.MQTTVersion = MQTTVERSION_3_1_1;
    if (result = MQTTClient_connect(mqttClient, &conn_opts) != MQTTCLIENT_SUCCESS)
    {
        log_error("MQTTClient_connect failed :%d", result);
        MQTTClient_destroy(&mqttClient);
        return ERROR;
    }

    /* 4.订阅主题 */
    if (MQTTClient_subscribe(mqttClient, SUBSCRIBE_TOPIC, QOS) != MQTTCLIENT_SUCCESS)
    {
        log_error("MQTTClient_subscribe failed");

        MQTTClient_disconnect(mqttClient, TIMEOUT);
        MQTTClient_destroy(&mqttClient);
        return ERROR;
    }

    return SUCCESS;
}

returnType_t app_mqtt_close(void)
{
    /* 取消订阅 */
    if (MQTTClient_unsubscribe(mqttClient, SUBSCRIBE_TOPIC) != MQTTCLIENT_SUCCESS)
    {
        log_error("MQTTClient_unsubscribe failed");
        return ERROR;
    }

    /* 断开连接 */
    if (MQTTClient_disconnect(mqttClient, TIMEOUT) != MQTTCLIENT_SUCCESS)
    {
        log_error("MQTTClient_disconnect failed");
        return ERROR;
    }

    /* 销毁客户端 */
    MQTTClient_destroy(&mqttClient);

    return SUCCESS;
}

returnType_t app_mqtt_publish(char *payload, int payloadlen)
{
    pubmsg.payload = payload;
    pubmsg.payloadlen = payloadlen;
    pubmsg.qos = QOS;

    if (MQTTClient_publishMessage(mqttClient, PUBLISH_TOPIC, &pubmsg, NULL) != MQTTCLIENT_SUCCESS)
    {
        log_error("MQTTClient_publishMessage failed");
        return ERROR;
    }

    return SUCCESS;
}

returnType_t app_mqtt_regRecvCb(mqtt_callback cb)
{
    mqtt_recvcallback_func = cb;

    if (mqtt_recvcallback_func == NULL)
        return ERROR;

    return SUCCESS;
}